/****************************************************************
 * Fulguro
 * Copyright (C) 2004 Christophe Clienti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 ***************************************************************/
#include <stdlib.h>

#if defined(__MINGW32__)
#if __GNUC__ < 4
  #include <malloc.h>
  #define FLGR_ALIGNED_MALLOC(ptr,size, align)  ptr = __mingw_aligned_malloc(size,align)
  #define FLGR_ALIGNED_FREE(p)                  __mingw_aligned_free(p)
#else
  #include <mm_malloc.h>
  #define FLGR_ALIGNED_MALLOC(ptr,size,align)   ptr = _mm_malloc(size,align)
  #define FLGR_ALIGNED_FREE(p)                  _mm_free(p)
#endif
#elif __GNUC__ < 4
  #define FLGR_ALIGNED_MALLOC(ptr,size, align)  posix_memalign (&ptr, alignment, size)
  #define FLGR_ALIGNED_FREE(p)                  free (ptr);
#else
  #include <mm_malloc.h>
  #define FLGR_ALIGNED_MALLOC(ptr,size,align)   ptr = _mm_malloc(size,align)
  #define FLGR_ALIGNED_FREE(p)                  _mm_free(p)
#endif

#include "flgrCoreErrors.h"
#include "flgrCoreMalloc.h"
#include "flgrCoreDispatch.h"


/*!
 * \addtogroup group_fulguro_core
 * @{
 */

//////////////////////////////////////////////////////////////////
/*! Allocation function
 *  @param size : size of the memory part allocated in octets
 *  @returns pointer to the start of the allocated zone
 */
//////////////////////////////////////////////////////////////////
void *flgr_malloc(size_t size) {
  void *tmp = malloc(size);

  FLGR_DECLARE_FUNCTION;

  if(tmp==NULL) {
    POST_ERROR("Could not allocate data, returning NULL pointer !\n");
    return NULL;
  }

  return tmp;
}


//////////////////////////////////////////////////////////////////
/*! Align allocation function
 *  @param size : size of the memory part allocated in octets
 *  @param alignment : alignment constraints in octets
 *  @returns pointer to the start of the allocated zone
 */
//////////////////////////////////////////////////////////////////
void *flgr_malloc_align(size_t size, size_t alignment) {
  void *tmp;

  FLGR_DECLARE_FUNCTION;

  FLGR_ALIGNED_MALLOC(tmp,size,alignment);

  if(tmp==NULL) {
    POST_ERROR("Could not allocate data, returning NULL pointer !\n");
    return NULL;
  }

  return tmp;

}

//////////////////////////////////////////////////////////////////
/*! Free allocation
 *  @param ptr : pointer on the first element of the allocated zone
 *  @returns nothing
 */
//////////////////////////////////////////////////////////////////
void flgr_free(void *ptr) {
  FLGR_DECLARE_FUNCTION;

  free(ptr);
}


//////////////////////////////////////////////////////////////////
/*! Free align allocation
 *  @param ptr : pointer on the first element of the allocated zone
 *  @returns nothing
 */
//////////////////////////////////////////////////////////////////
void flgr_free_align(void *ptr) {
  FLGR_DECLARE_FUNCTION;

  FLGR_ALIGNED_FREE(ptr);
}

//!@}
